/*
 * 文件名：OrderByInfoServiceImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-28
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.facility.room.roominfo.service.imp;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.facility.room.roominfo.dao.RoomInfoDao;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomInfo;
import com.wootide.cms.service.facility.room.roominfo.service.RoomInfoService;
import com.wootide.cms.service.facility.room.roominfo.transaction.RoomInfoTransaction;

/**
 * 预订方式业务层实现层
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 * @author ozm
 * @version 2013-1-28
 * @see RoomInfoServiceImp
 * @since 2013-1-28下午2:20:01
 */
public class RoomInfoServiceImp implements RoomInfoService
{
    // 注入运行日志
    private RunLog runLog;
    
    //用户事物层实例
    private RoomInfoTransaction roomInfoTransaction;
    
    //用户数据访问层实例
    private RoomInfoDao roomInfoDao;
    
    /**
     * 
     * Description: 分页查询
     * Implement: <br>
     * @param page 分页参数
     * @param queryConditon 查询条件
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午1:23:32
     */
    public Result queryForPages(Page page, QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryConditon = null == queryConditon ? new QueryCondition()
                    : queryConditon;
            queryConditon.setObjClass(RoomInfo.class);
            QueryResults queryResults = roomInfoTransaction.queryForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query OrderByInfo Success!");
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
            //数据库异常
            runLog.error("Query OrderByInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            e.printStackTrace();
            //异常
            runLog.error("Query OrderByInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    @Override
    public Result delRoomInfo(Integer[] innerIndexs, HttpServletRequest request)
    {

        // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            RoomInfo roomInfo = null;
            try
            {
                // 调用删除路由事务层
                roomInfo = roomInfoTransaction.delRoomInfo(innerIndex);
                runLog.info("Delete roomInfo Success! roomInfo = " + roomInfo,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete roomInfo Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete roomInfo Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete roomInfo Failed!  Innerindex = " + innerIndex,
                        e,
                        request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    
    }
    
    @Override
    public Result addRoomInfo(RoomInfo roomInfo, HttpServletRequest request)
    {

        // 打印方法入口参数
        runLog.debug("roomInfo=" + roomInfo);
        Result result = new Result();
        try
        {
            roomInfoTransaction.addRoomInfo(roomInfo);
            runLog.info("Add roomInfo Success! Input data:" + roomInfo, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Add ConsumablesInfo Failed! Input data:" + roomInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add roomInfo Failed! Input Data:" + roomInfo
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add roomInfo Failed!Input data:" + roomInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public RoomInfoTransaction getRoomInfoTransaction()
    {
        return roomInfoTransaction;
    }

    public void setRoomInfoTransaction(RoomInfoTransaction roomInfoTransaction)
    {
        this.roomInfoTransaction = roomInfoTransaction;
    }

    public RoomInfoDao getRoomInfoDao()
    {
        return roomInfoDao;
    }
    
    public void setRoomInfoDao(RoomInfoDao roomInfoDao)
    {
        this.roomInfoDao = roomInfoDao;
    }

    @Override
    public Result viewRoomInfo(Integer innerIndex)
    {

        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        
        Result result = new Result();
        try
        {
            RoomInfo roomInfo = roomInfoTransaction.viewRoomInfo(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(roomInfo);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One RoomInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod RoomInfo Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One RoomInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    
    }




    
}
